package cn.niit.letuseatdemo.controller.store;

import cn.niit.letuseatdemo.entity.domain.Classify;
import cn.niit.letuseatdemo.entity.groups.AddGroup;
import cn.niit.letuseatdemo.entity.groups.UpdateGroup;
import cn.niit.letuseatdemo.service.ClassifyService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 菜品分类控制器方法
 * @Author: su_fuming
 * @CreateTime： 2022/11/23 17:07
 */
@Slf4j
@RequestMapping("/store/classify")
@Controller
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    /**
     * @Description: 菜品分类管理（列表）页面的初始化及查询
     * @Author: su_fuming
     * @CreateTime: 2022/10/11 16:15 Unclosed character class near index 10
     * [\ [|\]|\s]
     * ^
     * @return: java.lang.String
     **/
    @GetMapping
    public String index(Model model, Classify classify) {
        model.addAttribute("classify", classify);
        List<Classify> classifyList = classifyService.selectClassifyList(classify);
        PageInfo<Classify> pageInfo = new PageInfo<>(classifyList);
        log.info("分页对象 = {}", pageInfo);

        log.info("查询出来的菜品分类列表：{}", classifyList);
        //第一个参数时该数据保存在域对象中的key,在页面中渲染数据时通过DepartmentList 来获取菜品集合
        model.addAttribute("classifyList", classifyList);
        model.addAttribute("pageInfo", pageInfo);
        return "store/classify/list";

    }

    /**
     * @Description: 接收用户请求，打开菜品分类添加、修改页面
     * 添加菜品分类、修改菜品分类共用一个页面
     * @Author: su_fuming
     * @CreateTime: 2022/10/13 9:20 Unclosed character class near index 10
     * [\ [|\]|\s]
     * ^
     * @return: java.lang.String
     **/
    @GetMapping("/{classifyId}")
    public String saveIndex(Model model, @PathVariable("classifyId") Long classifyId) {
        log.info("要修改的ID = " + classifyId);
        Classify classify = null;
        if (null != classifyId && classifyId > 0) { //值的修改
            classify = classifyService.selectClassifyByClassifyId(classifyId);
        }else{  //添加方法
            classify = new Classify();
        }
        model.addAttribute("classify", classify);
        return "store/classify/add";
    }

    /**
     * @Description: 新增保存菜品分类
     * @Author: su_fuming
     * @CreateTime: 2022/10/13 10:17 Unclosed character class near index 10
     * [\ [|\]|\s]
     * ^
     * @return: java.lang.String
     **/
    @PostMapping
    public String addSave(@Validated(AddGroup.class) Classify classify, BindingResult result, Model model){
        if (result.hasErrors()) {
            Map<String, String> errorsMap = new HashMap<>();
            for (FieldError error : result.getFieldErrors()) {
                log.error("参数校验错误:{}, {}", error.getField(), error.getDefaultMessage());
                errorsMap.put(error.getField(), error.getDefaultMessage());
            }
            model.addAttribute("errors", errorsMap);
            model.addAttribute("classify", classify);
        } else {
        int rows = classifyService.insertClassify(classify);
        if (rows == 1) {
            /**如果插入成功，则重定向至菜品分类列表页面*/
            return "redirect:/store/classify";
             }
        }
        return "store/classify/add";
    }

    /**
     * @Description: 根据id修改菜品分类信息
     * @Author: su_fuming
     * @CreateTime: 2022/10/13 13:38 Unclosed character class near index 10
     * [\ [|\]|\s]
     * ^
     * @return: java.lang.String
     **/
    @PutMapping
    public String editSave(@Validated(UpdateGroup.class) Classify classify, BindingResult result, Model model) {
        if (result.hasErrors()) {
            Map<String, String> errorsMap = new HashMap<>();
            for (FieldError error : result.getFieldErrors()) {
                log.error("参数校验错误:{},{}", error.getField(), error.getDefaultMessage());
                errorsMap.put(error.getField(), error.getDefaultMessage());
            }
            model.addAttribute("errors", errorsMap);
            model.addAttribute("classify", classify);
        }else {
            int rows = classifyService.updateClassify(classify);
            if (rows == 1) {
                /**如果插入成功，则重定向至菜品分类列表页面*/
                return "redirect:/store/classify";
            }
        }
        return "store/classify/add";
    }

    /**
     * @Description: 根据ID删除菜品分类信息
     * @Author: su_fuming
     * @CreateTime: 2022/10/13 15:05 Unclosed character class near index 10
    [\ [|\]|\s]
    ^
     * @return: java.lang.String
     **/
    @ResponseBody
    @DeleteMapping("/{classifyId}")
    public String delete(@PathVariable("classifyId") Long classifyId){
        int rows =classifyService.deleteClassifyByClassifyId(classifyId);
        if (rows == 1){
            return "true";
        }else{
            return "false";
        }
    }
}
